package com.wl.dzt.sys.core.dao.curd.sql.el;

import com.wl.dzt.sys.core.dao.select.term.Term;
import com.wl.dzt.sys.util.CollectionUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 动态拼接查询条件
 */
public class SqlFormat {

    /**
     * <b>创建一组条件</b>
     * <p>
     * 根据参数和规则 拼接sql语句
     * </p>
     *
     * @param term
     * @return String
     * @throws Exception
     */
    public static String builderWhere(Term term) throws Exception {
        String fmtSql = null;
        // 字符
        if (term.getField().contains("#string")) {
            fmtSql = " and %s %s '%s' ";
        }
        // 模糊
        else if (term.getField().contains("#like")) {
            fmtSql = " and %s %s '%%%s%%' ";
        }
        // 日期字符串
        else if (term.getField().contains("#date")) {
            term.setValue(term.getValue().toString().replace("T", " "));
            fmtSql = " and %s %s  '%s' ";
        }
        // in
        else if (term.getField().contains("#in")) {
            term.setOp("");
            term.setValue(buliderInData(term.getValue()));
            fmtSql = " and %s %s in  (%s) ";
        }
        // not in
        else if (term.getField().contains("#notIn")) {
            term.setOp("");
            term.setValue(buliderInData(term.getValue()));
            fmtSql = " and %s %s not in  (%s) ";
        }

        // is null
        else if (term.getField().contains("#isNull")) {
            fmtSql = " and %s is null ";
        }
        // is not null
        else if (term.getField().contains("#isNotNull")) {
            fmtSql = " and %s is not null ";
        }
        // 错误
        else {
            throw new Exception(term.getField() + " 不支持数据库字段类型!");
        }
        recoverTrem(term);

        return String.format(fmtSql, term.getField(), term.getOp(), term.getValue());
    }

    /**
     * <b>还原查询条件</b>
     * <p>
     * 特殊的sql表达式使用后还原查询条件以便正确的解析
     * </p>
     *
     * @param term
     */
    private static void recoverTrem(Term term) {
        term.setField(term.getField().replace("#string", "").replace("#like", "").replace("#date", "").replace("#in", "")
                .replace("#notIn", "").replace("#isNull", "").replace("#isNotNull", ""));
    }

    /**
     * <b>构建in</b>
     * <p>
     * 把in的数据设置为sql能接接受的格式
     * </p>
     *
     * @param val
     * @return String
     */
    private static String buliderInData(Object val) {
        String[] data = val.toString().split(",");
        List<String> arys = new ArrayList<String>();
        for (String row : data) {
            arys.add("'" + row + "'");
        }
        return CollectionUtil.join(arys, ",");
    }

    /**
     * <b>拼接查询条件</b>
     * <p>
     * 将查询条件组装在一起
     * </p>
     *
     * @param terms
     * @return String
     * @throws Exception
     */
    public static String builderWheres(List<Term> terms) throws Exception {
        StringBuilder builder = new StringBuilder();
        for (Term term : terms) {
            builder.append(builderWhere(term));
        }
        return builder.toString();
    }
}
